package com.hrt.manage.order.service.impl;


import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.HRTMember;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.OrderInfor;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.manage.order.dao.OrderInforDao;
import com.hrt.manage.order.service.OrderDetailService;
import com.hrt.manage.order.service.OrderInforService;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.vo.OrderDetailVO;
import com.hrt.vo.OrderInforVO;
@Transactional
@Service
public class OrderInforServiceImpl implements OrderInforService{
	@Autowired 
	private OrderInforDao orderInforDao;
	@Autowired 
	private HRTMemberService hrtMemberService;
	@Autowired 
	private GiftDetailService giftDetailService;
	@Autowired 
	private OrderDetailService orderDetailService;
	@Override
	public Long totalCount() {
		return orderInforDao.totalCount();
	}

	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<OrderInfor> delResult = orderInforDao.getByIds(ids);
		for (OrderInfor  gi: delResult) {
			gi.setIsdelete(new Integer(1));
			orderInforDao.updateEntity(gi);
		}
		
	}

	@Override
	public void saveOrder(OrderInfor orderInfor) throws Exception {
		orderInforDao.saveEntity(orderInfor);
		
	}

	@Override
	public List queryByPage(int pageIndex, int pageSize) {
		return orderInforDao.queryByPage(pageIndex, pageSize);
	}

	

	@Override
	public OrderInfor getOrderByid(Long orderId) {
		return orderInforDao.getById(orderId);
	}

	@Override
	public List<OrderInfor> loadOrders() {
		return null;
		
	}

	@Override
	public void updateOrder(OrderInfor orderInfor) {
		orderInforDao.updateEntity(orderInfor);
		
	}
	/**
	 * 获取数据库当前时间
	 * @return
	 */
	@Override
	public Timestamp getDate() {
		return orderInforDao.getDate();
	}

	/**
	 * 
	 * 发货
	 */
	@Override
	public void sendGoodsById(Long[] ids) throws Exception {
		List<OrderInfor> infors = orderInforDao.getByIds(ids);
		for(OrderInfor  oi: infors){
			oi.setStatus(new Integer(2));
			orderInforDao.updateEntity(oi);
		}
		
	}
	/**
	 * 退货
	 */
	@Override
	public void returnGoodsById(Long[] ids) throws Exception {
		List<OrderInfor> infors = orderInforDao.getByIds(ids);
		for(OrderInfor  oi: infors){
			oi.setStatus(new Integer(6));
			orderInforDao.updateEntity(oi);
		}
		
	}
	/**
	 * 退款
	 */
	@Override
	public void returnMoneyById(Long[] ids) throws Exception {
		List<OrderInfor> infors = orderInforDao.getByIds(ids);
		for(OrderInfor  oi: infors){
			oi.setStatus(new Integer(7));
			orderInforDao.updateEntity(oi);
		}
		
	}
	/**
	 *将OrderInforVO转换为OrderInfor
	 *实现添加功能
	 * @param vo
	 * @return
	 */
	@Override
	public OrderInfor saveOrderInfor(OrderInforVO vo,List<OrderDetailVO> ods) {
		OrderInfor orderInfor = new OrderInfor();
		try {
			BeanUtils.copyProperties(orderInfor, vo);
			HRTMember member = hrtMemberService.getMemberByName(orderInfor.getMemberName());
			orderInfor.setHrtMember(member);
			for(OrderDetailVO od:ods){
				OrderDetail newDetail = new OrderDetail();
				BeanUtils.copyProperties(newDetail, od);
				
				// 根据礼品规格id获取礼品规格实体
				GiftDetail giftDetail = giftDetailService.getById(od.getGiftDetailId());
				this.saveOrder(orderInfor);
				newDetail.setOrderInfor(orderInfor);
				newDetail.setGiftDetail(giftDetail);
				orderDetailService.saveOrderDetail(newDetail);
			};
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return orderInfor;
	}
	/**
	 *将OrderInforVO转换为OrderInfor
	 *实现修改功能
	 * @param vo
	 * @return
	 */
	@Override
	public void updateOrderInfor(OrderInforVO vo,List<OrderDetailVO> ods) {
		// 根据会员名查找会员实体

					HRTMember hrtMember = this.hrtMemberService
							.getMemberByName(vo.getMemberName());
					OrderInfor orderInfor = this.getOrderByid(vo.getId());
					try {
						BeanUtils.copyProperties(orderInfor, vo);
						orderInfor.setHrtMember(hrtMember);
						this.updateOrder(orderInfor);
						for(OrderDetailVO od:ods){
							OrderDetail newDetail=null;
							if(od.getId()==null){
								
								newDetail = new OrderDetail();
								BeanUtils.copyProperties(newDetail, od);
								// 根据礼品id获取礼品实体
								GiftDetail giftDetail = giftDetailService.getById(od.getGiftDetailId());
								
								newDetail.setOrderInfor(orderInfor);
								newDetail.setGiftDetail(giftDetail);
								orderDetailService.saveOrderDetail(newDetail);
							}
							else{
							newDetail = orderDetailService.getOrderDetailByid(od.getId());
							od.setIsdelete(0);
							od.setCreateTime(newDetail.getCreateTime());
							od.setCreator(newDetail.getCreator());
							od.setEditor(newDetail.getEditor());
							od.setEditTime(newDetail.getEditTime());
							BeanUtils.copyProperties(newDetail, od);
							orderDetailService.updateOrderDetail(newDetail);
							}
						};
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}
		
	}

	/**
	 * 根据时间查询订单
	 */
	@Override
	public List<OrderInfor> getOrderByTime(java.sql.Date startTime, java.sql.Date endTime) {
		
		return orderInforDao.getOrderByTime(startTime, endTime);
	}
}
